home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / basics / alwayspreview / alwayspreview.c next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  6.0 KB  |  220 lines

  1. /*
  2.     File:        AlwaysPreview.c
  3.  
  4.     Contains:    This simple app demonstrates how to force the preview enabled mode by install a modal
  5.                 dialog filter proc in the CustomGetFilePreview routine. This works with System 7 call.
  6.                 You should check out the Standard File documentation in Inside Mac: Files as well.
  7.  
  8.                 This sample is based on CustomPreview code in QT1.0 CD.
  9.  
  10.     Written by: Larry Lai    
  11.  
  12.     Copyright:    Copyright © 1995-1999 by Apple Computer, Inc., All Rights Reserved.
  13.  
  14.                 You may incorporate this Apple sample source code into your program(s) without
  15.                 restriction. This Apple sample source code has been provided "AS IS" and the
  16.                 responsibility for its operation is yours. You are not permitted to redistribute
  17.                 this Apple sample source code as "Apple sample source code" after having made
  18.                 changes. If you're going to re-distribute the source, we require that you make
  19.                 it clear in the source that the code was descended from Apple sample source
  20.                 code, but that you've made changes.
  21.  
  22.     Change History (most recent first):
  23.                 7/28/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  24.                 
  25.  
  26. */
  27.  
  28. #include "ImageCompression.h"
  29. #include <Files.h>
  30. #include <GestaltEqu.h>
  31. #include <StandardFile.h>
  32. #include <Controls.h>
  33. #include <Fonts.h>
  34. #include <ToolUtils.h>
  35.  
  36. // some constants for the buttons we added to the dialog
  37. enum {
  38.     buttonPictureFiles = 16,
  39.     buttonAllFiles
  40. };
  41.  
  42. // prototypes
  43. void updateFileButtons(DialogPtr d, Boolean showAllFiles);
  44. pascal short myDialogHook7(short item, DialogPtr d, Boolean *showAllFiles);
  45. pascal Boolean myFileFilter7(CInfoPBPtr pb, Boolean *showAllFiles);
  46. Boolean doCustom7(FSSpec *fss);
  47. pascal    Boolean    myModalFilter7(DialogPtr theDlg, EventRecord *theEvent, short *itemHit, Boolean *showAllFiles);
  48.  
  49.  
  50. /*
  51.     resource id numbers of the dialogs.
  52.  
  53.     Note that both of these dialogs have a Dialog Color Table ('dctb')
  54.     resource as well. This is so that the window used by Standard File will
  55.     be a color window. In some cases this may allow previews to display more
  56.     accurate colors. On System 7, owing to a bug in the PopUp menu CDEF, it
  57.     is necessary to make it a color window, or else the CDEF draws incorrectly
  58.     in some cases (when the port origin is not (0,0)).
  59. */
  60. #define Custom7DialogID (129)
  61.  
  62. // a global to keep track of whether we are showing all files or just pictures
  63. Boolean gShowAllFiles = false;
  64.  
  65. Boolean doCustom7(FSSpec *fss);            // returns true if a file was selected
  66.  
  67. /*
  68.      routine shared by both the system 6 and system 7 filters. 
  69.      this routine simply hilites the two radio buttons depending on
  70.          the value of the "showAllFiles" parameter.
  71. */
  72. void updateFileButtons(DialogPtr d, Boolean showAllFiles)
  73. {
  74.     short kind;
  75.     Rect r;
  76.     ControlHandle ch;
  77.  
  78.     GetDialogItem(d, buttonPictureFiles, &kind, (Handle *)&ch, &r);
  79.     SetControlValue(ch, !showAllFiles);
  80.  
  81.     GetDialogItem(d, buttonAllFiles, &kind, (Handle *)&ch, &r);
  82.     SetControlValue(ch, showAllFiles);
  83. }
  84.  
  85.  
  86. /*****************************************************
  87.  
  88.     System 7 Preview code
  89.  
  90.     Note that the system 7 standard file interface allows for a
  91.         YDPtr (YourDataPtr) to be passed to all filters. This means
  92.         that the filter procs do not need to use any global
  93.         variables.
  94.  
  95. ******************************************************/
  96.  
  97. pascal short myDialogHook7(short item, DialogPtr d, Boolean *showAllFiles)
  98. {
  99.  
  100.     switch (item) {
  101.         case sfHookFirstCall:
  102.                 // put buttons in correct initial state
  103.                 updateFileButtons(d, *showAllFiles);
  104.                 break;
  105.         case buttonPictureFiles:
  106.                 if (*showAllFiles) {
  107.                     *showAllFiles = false;
  108.                     updateFileButtons(d, *showAllFiles);
  109.                     item = sfHookRebuildList;    // force standard file to redisplay list
  110.                 }
  111.                 break;
  112.         case buttonAllFiles:
  113.                 if (!*showAllFiles) {
  114.                     *showAllFiles = true;
  115.                     updateFileButtons(d, *showAllFiles);
  116.                     item = sfHookRebuildList;    // force standard file to redisplay list
  117.                 }
  118.                 break;
  119.     }
  120.  
  121.     return(item);
  122. }
  123.  
  124. pascal Boolean myFileFilter7(CInfoPBPtr pb, Boolean *showAllFiles)
  125. {
  126.     /*
  127.         return false to indicate that file/directory should be displayed.
  128.         note that unlike the system 6 version of this filter, you can suppress the
  129.         display of directories as well as files. thus the extra check for directories
  130.         is required below
  131.     */
  132.  
  133.     if (*showAllFiles)
  134.         return(false);
  135.     else {
  136.         if (pb->hFileInfo.ioFlFndrInfo.fdType == 'PICT')
  137.             return(false);
  138.         else
  139.             return(!(pb->hFileInfo.ioFlAttrib & 16));        // directory
  140.     }
  141. }
  142.  
  143. pascal    Boolean    myModalFilter7(DialogPtr theDlg, EventRecord *theEvent, short *itemHit, Boolean *showAllFiles)
  144. {
  145.     short    iType;
  146.     Handle    iHandle;
  147.     Rect    iRect;
  148.     short    c;
  149.     
  150.     *showAllFiles = true;
  151.     
  152.     /*
  153.         handle null event, set the control value of show preview checkbox to 1 if it is 
  154.         unchecked. event from ModalDialog will pass here first before it gets handed to
  155.         the standard file package filter
  156.         
  157.     */
  158.  
  159.     switch (theEvent->what) {
  160.         case nullEvent:
  161.             *itemHit = 15;  // 15 is the resource ID of show preview checkbox
  162.             GetDialogItem(theDlg, *itemHit, &iType, &iHandle, &iRect);
  163.             c = GetControlValue ((ControlHandle) iHandle);
  164.     
  165.             if (!c){
  166.                 SetControlValue((ControlHandle)iHandle, true);
  167.             }
  168.             else
  169.                 return false;
  170.                 
  171.             return true;
  172.             break;
  173.         default:
  174.             return false;
  175.     }
  176. }
  177.         
  178. Boolean doCustom7(FSSpec *fss)
  179. {
  180.     #pragma unused(fss)
  181.     Point where;
  182.     StandardFileReply reply;
  183.  
  184.     where.h = where.v = -2;                // center the dialog on the "best" screen
  185.  
  186.     CustomGetFilePreview(NewFileFilterYDProc(myFileFilter7), -1, 0, &reply, Custom7DialogID, where,
  187.                             NewDlgHookYDProc(myDialogHook7), NewModalFilterYDProc(myModalFilter7), 0, 0, &gShowAllFiles);
  188.  
  189.     return(reply.sfGood);
  190. }
  191.  
  192. /*
  193.     Simple main routine.
  194.  
  195. */
  196. void main(void)
  197. {
  198.     long response;
  199.     FSSpec fileSpec;
  200.  
  201.     // initialize the world
  202.     InitGraf(&qd.thePort);
  203.     InitFonts();
  204.     InitWindows();
  205.     InitMenus();
  206.     TEInit();
  207.     InitDialogs(0L);
  208.     InitCursor();
  209.     MaxApplZone();
  210.  
  211.     // must have image compression manager to use Standard Preview
  212.     if (Gestalt(gestaltCompressionMgr, &response))
  213.         return;
  214.  
  215.     // system 7 calls are only available if the standard file selectors 5 through 8 are around
  216.     if ( (Gestalt(gestaltStandardFileAttr, &response) == noErr) &&
  217.         BitTst((Ptr)&response, 31 - gestaltStandardFile58) )
  218.         doCustom7(&fileSpec);
  219. }
  220.